﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TPMS_ScanComparison;

namespace TPMS_ScanComparison.Helper
{
    public static class SimpleSerial
    {
        public static class SerialGenerator
        {
            private static readonly object lockObject = new object();
            private const string DataFile = "multiserials.dat";

            // 存储三个独立序列的当前值
            private static Dictionary<string, int> serialCounters = new Dictionary<string, int>();
            private static DateTime lastUpdateDate = DateTime.Today;

            static SerialGenerator()
            {
                LoadSerialData();
            }

            /// <summary>
            /// 加载序列数据
            /// </summary>
            private static void LoadSerialData()
            {
                // 从全局配置获取初始值
                int initialNumber = Global.Number;
                
                // 初始化三个序列，使用全局Number参数作为起始值
                serialCounters["SerialA"] = initialNumber;
                serialCounters["SerialB"] = initialNumber;
                serialCounters["SerialC"] = initialNumber;

                // 如果数据文件存在，读取数据
                if (File.Exists(DataFile))
                {
                    try
                    {
                        string[] lines = File.ReadAllLines(DataFile);
                        if (lines.Length >= 4)
                        {
                            // 第一行是日期
                            if (DateTime.TryParse(lines[0], out DateTime savedDate))
                            {
                                lastUpdateDate = savedDate;

                                // 检查日期是否变化
                                if (lastUpdateDate.Date != DateTime.Today)
                                {
                                    // 日期变化，重置所有计数器
                                    return;
                                }

                                // 读取三个序列的值
                                if (lines.Length >= 4)
                                {
                                    if (int.TryParse(lines[1], out int serialA)) serialCounters["SerialA"] = serialA;
                                    if (int.TryParse(lines[2], out int serialB)) serialCounters["SerialB"] = serialB;
                                    if (int.TryParse(lines[3], out int serialC)) serialCounters["SerialC"] = serialC;
                                }
                            }
                        }
                    }
                    catch
                    {
                        // 如果读取失败，使用默认值
                    }
                }
            }

            /// <summary>
            /// 保存序列数据
            /// </summary>
            private static void SaveSerialData()
            {
                try
                {
                    File.WriteAllLines(DataFile, new string[] {
                    DateTime.Today.ToString("yyyy-MM-dd"),
                    serialCounters["SerialA"].ToString(),
                    serialCounters["SerialB"].ToString(),
                    serialCounters["SerialC"].ToString()
                });
                }
                catch
                {
                    // 保存失败处理
                }
            }

            /// <summary>
            /// 获取序列A的下一个流水号
            /// </summary>
            public static string NextSerialA()
            {
                lock (lockObject)
                {
                    // 检查日期是否变化
                    if (lastUpdateDate.Date != DateTime.Today)
                    {
                        // 日期变化，重置所有计数器到全局Number值
                        int resetValue = Global.Number;
                        serialCounters["SerialA"] = resetValue;
                        serialCounters["SerialB"] = resetValue;
                        serialCounters["SerialC"] = resetValue;
                        lastUpdateDate = DateTime.Today;
                    }

                    serialCounters["SerialA"]++;
                    SaveSerialData();
                    return serialCounters["SerialA"].ToString("D4");
                }
            }

            /// <summary>
            /// 获取序列B的下一个流水号
            /// </summary>
            public static string NextSerialB()
            {
                lock (lockObject)
                {
                    // 检查日期是否变化
                    if (lastUpdateDate.Date != DateTime.Today)
                    {
                        // 日期变化，重置所有计数器到全局Number值
                        int resetValue = Global.Number;
                        serialCounters["SerialA"] = resetValue;
                        serialCounters["SerialB"] = resetValue;
                        serialCounters["SerialC"] = resetValue;
                        lastUpdateDate = DateTime.Today;
                    }

                    serialCounters["SerialB"]++;
                    SaveSerialData();
                    return serialCounters["SerialB"].ToString("D4");
                }
            }

            /// <summary>
            /// 获取序列C的下一个流水号
            /// </summary>
            public static string NextSerialC()
            {
                lock (lockObject)
                {
                    // 检查日期是否变化
                    if (lastUpdateDate.Date != DateTime.Today)
                    {
                        // 日期变化，重置所有计数器到全局Number值
                        int resetValue = Global.Number;
                        serialCounters["SerialA"] = resetValue;
                        serialCounters["SerialB"] = resetValue;
                        serialCounters["SerialC"] = resetValue;
                        lastUpdateDate = DateTime.Today;
                    }

                    serialCounters["SerialC"]++;
                    SaveSerialData();
                    return serialCounters["SerialC"].ToString("D4");
                }
            }

            /// <summary>
            /// 重置所有序列
            /// </summary>
            public static void ResetAll()
            {
                lock (lockObject)
                {
                    // 重置到全局Number值
                    int resetValue = Global.Number;
                    serialCounters["SerialA"] = resetValue;
                    serialCounters["SerialB"] = resetValue;
                    serialCounters["SerialC"] = resetValue;
                    SaveSerialData();
                }
            }

            /// <summary>
            /// 获取所有序列的当前状态
            /// </summary>
            public static string GetStatus()
            {
                return $"序列A: {serialCounters["SerialA"]:D4}, 序列B: {serialCounters["SerialB"]:D4}, 序列C: {serialCounters["SerialC"]:D4}";
            }
        }
    }
}
